﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DevComponents.DotNetBar;

namespace QLST
{
    public partial class frmHoaDon : Form
    {
        HoaDonBUS cls =new HoaDonBUS() ;
        HoaDonDTO p = new HoaDonDTO();
        Boolean  flag =true ;
        public frmHoaDon()
        {
            InitializeComponent();
        }

        private void frmHoaDon_Load(object sender, EventArgs e)
        {
            ClearTextBox();
            LockTextBox(false);
            LockButton(true);
            loadHD();
        }

        public void loadHD()
        {
            gvHoaDon.DataSource = cls.Load_HoaDon();
        }

        private void btnNVThem_Click(object sender, EventArgs e)
        {
            LockButton(false);
            LockTextBox(true);
            ClearTextBox();
            gvHoaDon.Enabled = false;
            txtSoLuong.Focus();
            cmbMaKH.DataSource = cls.KH();
            cmbMaKH.DisplayMember = "MaKH";
            cmbMaKH.ValueMember = "MaKH";
            cmbMaNV.DataSource = cls.NV();
            cmbMaNV.DisplayMember = "MaNV";
            cmbMaNV.ValueMember = "MaNV";
            cmbMaSP.DataSource = cls.SP();
            cmbMaSP.DisplayMember = "MaSp";
            cmbMaSP.ValueMember = "GiaBan";
            txtGiaBan.Text = cmbMaSP.SelectedValue.ToString();
        }
        
        private void cmbMaSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbMaSP.DisplayMember = "MaSp";
            cmbMaSP.ValueMember = "GiaBan";
            txtGiaBan.Text = cmbMaSP.SelectedValue.ToString();
        }
        private void btnNVXoa_Click(object sender, EventArgs e)
        {
            p.MaHD = txtMaHD.Text;

            cls.Del_HoaDon(p);
            frmHoaDon_Load(sender, e);
            
        }

        private void btnNVSua_Click(object sender, EventArgs e)
        {
            LockTextBox(true);
            LockButton(false);
            gvHoaDon.Enabled = false;
            txtSoLuong.Focus();           
        }



        private void gvHoaDon_SelectionChanged(object sender, EventArgs e)
        {
            txtMaHD.Text = gvHoaDon.CurrentRow.Cells["colMaHD"].Value.ToString();
            cmbMaSP.Text = gvHoaDon.CurrentRow.Cells["colMaSP"].Value.ToString();
            cmbMaNV.Text = gvHoaDon.CurrentRow.Cells["colMaNV"].Value.ToString();
            cmbMaKH.Text = gvHoaDon.CurrentRow.Cells["colMaKH"].Value.ToString();
            txtSoLuong.Text = gvHoaDon.CurrentRow.Cells["colSoLuong"].Value.ToString();
            txtGiaBan.Text = gvHoaDon.CurrentRow.Cells["colGiaBan"].Value.ToString();
            txtTongTien.Text = gvHoaDon.CurrentRow.Cells["colTongTien"].Value.ToString();
            dateNgayMua.Text = gvHoaDon.CurrentRow.Cells["colNgayMua"].Value.ToString();
        }

        private void btnNVTim_Click(object sender, EventArgs e)
        {
            p.MaHD = txtTimKiem .Text ;
            p.MaKH = txtTimKiem .Text ;
            if(cbMaHD .Checked ==true)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            gvHoaDon.ClearSelection();
            try
            {
                gvHoaDon.DataSource = cls.TimHD(p, flag);
            }
            catch
            {
                MessageBoxEx.Show("Bạn nhập sai Mã hóa đơn hoặc mã khách hàng", "PASSWORD CHANGED", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void btnNVHuy_Click(object sender, EventArgs e)
        {
            ClearGV();
            txtTimKiem.Text = "";
            frmHoaDon_Load(sender, e);
            LockButton(true );
            LockTextBox(false);
            gvHoaDon.Enabled = true;
        }

        private void btnNVLuu_Click(object sender, EventArgs e)
        {
            if (txtMaHD.Text == "")
            {
                flag = true ;
            }
            else if(txtMaHD .Text !="")
            {
                flag = false;
            }
            p.MaSP = cmbMaSP.Text;
            p.MaKH = cmbMaKH.Text;
            p.MaNV = cmbMaNV.Text;
            p.SoLuong = float.Parse(txtSoLuong.Text);
            p.GiaBan = float.Parse(txtGiaBan.Text);
            p.TongTien = float.Parse(txtTongTien.Text);
            p.NgayMua = dateNgayMua.Value;
            cls.IsUp_HoaDon(p, flag);
            ClearGV();
            frmHoaDon_Load(sender, e);
            gvHoaDon.Enabled = true;
            MessageBoxEx.Show("Cập nhật thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ClearGV()
        {
            gvHoaDon.ClearSelection();
        }

        private void ClearTextBox()
        {
            txtMaHD.Text = "";
            txtSoLuong.Text = "";
            txtGiaBan.Text = "";
            txtSoLuong.Text = "";
            txtTongTien.Text = "";
            dateNgayMua.Text = "";
        }

        private void LockButton(Boolean  f)
        {
            btnNVThem.Enabled = f;
            btnNVSua.Enabled = f;
            btnNVXoa.Enabled = f;
            btnNVLuu.Enabled = !f;
            btnNVHuy.Enabled = !f;
        }

        private void LockTextBox(Boolean f)
        {
            txtMaHD.Enabled = f;
            txtSoLuong.Enabled = f;
            txtGiaBan.Enabled = f;
            txtTongTien.Enabled = f;
            cmbMaKH.Enabled = f;
            cmbMaNV.Enabled = f;
            cmbMaSP.Enabled = f;
            dateNgayMua.Enabled = f;
            if (f == false)
            {
                txtMaHD.BackColor = Color.LemonChiffon;
                txtSoLuong.BackColor = Color.LemonChiffon;
                txtGiaBan.BackColor = Color.LemonChiffon;
                txtTongTien.BackColor = Color.LemonChiffon;
                cmbMaKH.BackColor = Color.LemonChiffon;
                cmbMaNV.BackColor = Color.LemonChiffon;
                cmbMaSP.BackColor = Color.LemonChiffon;
                dateNgayMua.BackColor = Color.LemonChiffon;
            }
            else if (f==true )
            {
                txtMaHD.BackColor = Color.White ;
                txtSoLuong.BackColor = Color.White;
                txtGiaBan.BackColor = Color.White;
                txtTongTien.BackColor = Color.White;
                cmbMaKH.BackColor = Color.White;
                cmbMaNV.BackColor = Color.White;
                cmbMaSP.BackColor = Color.White;
                dateNgayMua.BackColor = Color.White;
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            txtTimKiem.Text = "";
            frmHoaDon_Load(sender, e);
        }
    }
}
